<?php

namespace app\admin\model;
use think\Request;
use think\Session;
use think\Model;
use think\Db;

class StaffsModel extends Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function users()
    {
        return Db::table('staffs')->select();
    }

    //管理员修改密码
    public function editPass($staffId)
    {
        $login_pass = trim(input("newPass"));
        if ($login_pass == '') AppReturn('密码不能为空');
        $staff = Db::table('staffs')->where('staff_id', $staffId)
            ->field('login_pass,secret_key')->find();
        if (empty($staff)) AppReturn('修改失败');
        $oldPass = md5(input('oldPass') . $staff['secret_key']);
        if ($oldPass != $staff['login_pass']) {
            return AppReturn('原始密码错误');
        } else {
            $login_pass = md5(input("post.newPass") . $staff['secret_key']);
            $result = Db::table('staffs')->where('staff_id', $staffId)->update(['login_pass' => $login_pass]);
            if (false !== $result) {
                return AppReturn("修改成功", 1);
            } else {
                return AppReturn('修改失败', -1);
            }
        }
    }

    //修改用户状态是否有权登录
    public function setStatus($staff_id, $s)
    {
        //0停用
        $db = Db::table('staffs');
        if ($s == 0) {
            $res = $db->where('staff_id', $staff_id)->update(['staff_status' => 0]);
            return $res;
        } else {
            $res = $db->where('staff_id', $staff_id)->update(['staff_status' => 1]);
            return $res;
        }


    }

    //获取用户数据
    public function user($login_name)
    {
        $where = " AND login_name like '%$login_name%'";
        if (empty($where)) {
            $staff = Db::table('staffs')->where("is_del=0")->select();
            return $staff;
        } else {
            $staff = Db::table('staffs')->where("is_del=0 $where")->select();
            return $staff;
        }

    }

    public function del($staff_id)
    {
        return Db::table('staffs')->where('staff_id', $staff_id)->update(['is_del' => '1']);
    }

    //用户个人编辑
    public function userEdit($staff_id)
    {
        $db = Db::table('staffs');
        return $res = $db->where('staff_id', $staff_id)->select();
    }

    //修改成功后返回的数据
    public function toEdit()
    {
        $data = [
            'staff_id' => input('staff_id'),
            'create_time' => time(),
            'login_name' => input('login_name'),
            'sex' => input('sex'),
            'staff_name' => input('staff_name'),
            'message' => input('message')
        ];
        $db = Db::table('staffs');
        return $res = $db->where('staff_id', $data['staff_id'])->update($data);
    }

    //用户详情
    public function userDetail($staff_id)
    {
        $db = Db::table('staffs');
        return $res = $db->where('staff_id', $staff_id)->select();
    }

    //修改密码
    public function usersEditPwd()
    {
        if (Request::instance()->isPost()) {
            $staff_id = input('staff_id');
            $login_pass = trim(input("post.newPass"));
            if ($login_pass == '') AppReturn('密码不能为空');
            $staff = Db::table('staffs')
                ->where('staff_id', $staff_id)
                ->field('login_pass,secret_key')
                ->find();
            if (empty($staff)) AppReturn('修改失败');
            $oldPass = md5(input('post.oldPass') . $staff['secret_key']);
            if ($oldPass != $staff['login_pass']) {
                return AppReturn('原始密码错误');
            } else {
                $login_pass = md5(input("post.newPass") . $staff['secret_key']);
                $result = Db::table('staffs')->where('staff_id', $staff_id)->update(['login_pass' => $login_pass]);
                if (false !== $result) {
                    return AppReturn("修改成功", 1);
                } else {
                    return AppReturn('修改失败', -1);
                }
            }
        } else {
            $db = Db::table('staffs');
            $staff_id = input('id');
            return $res = $db->where('staff_id', $staff_id)->select();
        }
    }

    //批量删除
    public function delAll($id)
    {
        $db = Db::table('staffs');
        $re = $db->where('staff_id', $id)->update(['is_del' => 1]);
        return $re;
    }

    //用户登录获取权限
    public function checkLogin()
    {
        $loginName = input("loginName");
        $loginPass = input("loginPass");
        $data = [
            'login_name' => $loginName,
        ];
        $staffs = Db::table('staffs')->where($data)->find();
        $staff_id = $staffs['staff_id'];
        $pwd = md5($loginPass . $staffs['secret_key']);
        if ($staffs != false) {
            $res = Db::table('staffs')
                ->alias('a')
                ->join('auth_group_access b', 'a.staff_id=b.uid', 'left')
                ->join('auth_group c', 'c.id=b.group_id', 'left')
                ->where('staff_id', $staff_id)
                ->select();
            $rules = '';
            foreach ($res as $v) {
                $rules = $rules . $v['rules'] . ",";
            }
            $rules = substr($rules, 0, -1);
            $arr = explode(',', $rules);
            $rule = array_unique($arr);
            $rule = array_filter($rule);
        }
        //判断用户账号是否被冻结 0停用
        if (empty($staffs)) {
            return 1;
        }
        if ($staffs['staff_status'] == 0) {
            return 2;
            //登录成功
        } elseif (($loginName != $staffs['login_name'] || $staffs['login_pass'] != $pwd)) {
            return 1;

        } elseif ($loginName == $staffs['login_name'] && $staffs['login_pass'] == $pwd) {
            $data = [
                'last_time' => time(),
                'last_ip' => request()->ip()
            ];
            Db::table('staffs')->where('staff_id', $staff_id)->update($data);
            Session::set('STAFF', $staffs);
            Session::set('RULES', $rule);
            return 3;
        }
    }

    public function edit()
    {
        $id = input('id');
        return Db::table('staffs')->where('staff_id', $id)->find();
    }
    //修改个人信息
    public function editMsg(){
        $id = (int)input('id');
        $data = [
            'login_name' =>input('loginName'),
            'staff_no' =>input('no'),
            'staff_photo'=>input('staffPhoto'),
            'work_status'=>input('workStatus'),
            'staff_status'=>input('staffStatus')
        ];
        return Db::table('staffs')->where('staff_id',$id)->update($data);
    }
}
